package io.github.uicdb.liba.api.register.codec;


import com.google.gson.*;
import net.minecraft.core.NonNullList;
import net.minecraft.world.item.crafting.Ingredient;

import java.lang.reflect.Type;

public class NonnullIngredientListCodec implements JsonSerializer<NonNullList<Ingredient>>, JsonDeserializer<NonNullList<Ingredient>> {
    @Override
    public NonNullList<Ingredient> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        NonNullList<Ingredient> nonNullList=NonNullList.create();
        json.getAsJsonArray().forEach(jsonElement -> {
            nonNullList.add(context.deserialize(jsonElement, Ingredient.class));
        });
        return nonNullList;
    }

    @Override
    public JsonElement serialize(NonNullList<Ingredient> src, Type typeOfSrc, JsonSerializationContext context) {
        JsonArray jsonArray=new JsonArray();
        src.forEach(ingredient -> {
            jsonArray.add(context.serialize(ingredient));
        });
        return jsonArray;
    }
}
